#include "messagepackage.h"

messagePackage::messagePackage()
{

}

void messagePackage::setMessage(const QJsonObject &value)
{
    message = value;
}

void messagePackage::setMessage(const QByteArray byte)
{
    message = QJsonDocument::fromJson(byte).object();
}

void messagePackage::setMessage(const QString str)
{
    message = QJsonDocument::fromJson(str.toUtf8()).object();
}

void messagePackage::setType(messagePackage::Type type)
{
    message["type"] = type;
}

messagePackage::Type messagePackage::type() const
{
    return static_cast<Type>(message["type"].toInt());
}

void messagePackage::setUserName(QString name)
{
    message["userName"] = name;
}

QString messagePackage::userName()
{
    return message["userName"].toString();
}

void messagePackage::setUserPassword(QString pswd)
{
    message["userPassword"] = pswd;
}

QString messagePackage::userPassword()
{
    return message["userPassword"].toString();
}

QByteArray messagePackage::toByteArray() const
{
    QJsonDocument doc(message);
    return doc.toJson();
}


void messagePackage::setRet(bool ret)
{
    message["return"] = ret;
}

bool messagePackage::ret() const
{
    return message["return"].toBool();
}

void messagePackage::setRetInt(int value)
{
    message["retInt"] = value;
}

int messagePackage::retInt() const
{
    return message["retInt"].toInt();
}

void messagePackage::setOnlineUsers(QStringList users)
{
    QJsonObject json;
    for (auto user : users)
    {
        json.insert(user, user);
    }
    message["onlineUsers"] = json;
}

QStringList messagePackage::OnlinUsers() const
{
    QJsonObject json = message["onlineUsers"].toObject();
    return json.keys();
}

void messagePackage::setSender(QString sender)
{
    message["sender"] = sender;
}

QString messagePackage::sender() const
{
    return message["sender"].toString();
}

void messagePackage::setReceiver(QString receiver)
{
    message["receiver"] = receiver;
}

QString messagePackage::receiver() const
{
    return message["receiver"].toString();
}

void messagePackage::setInfo(QString info)
{
    message["info"] = info;
}

QString messagePackage::info() const
{
    return message["info"].toString();
}

void messagePackage::setTime(QString time)
{
    message["time"] = time;
}

QString messagePackage::time() const
{
    return message["time"].toString();
}

void messagePackage::setAllUsers(QStringList users)
{
    QJsonObject json;
    for (auto user : users)
    {
        json.insert(user, user);
    }
    message["allUsers"] = json;
}

QStringList messagePackage::allUsers() const
{
    QJsonObject json = message["allUsers"].toObject();
    return json.keys();
}

void messagePackage::setGroupName(QString name)
{
    message["groupName"] = name;
}

QString messagePackage::groupName() const
{
    return message["groupName"].toString();
}

void messagePackage::setFileName(QString name)
{
    message["fileName"] = name;
}

QString messagePackage::fileName() const
{
    return message["fileName"].toString();
}

void messagePackage::setFileData(QByteArray data)
{
    QString filedata = data.toBase64();
    message["fileData"] = filedata;
}

QByteArray messagePackage::fileData() const
{
//    QString data = message["fileData"].toString();
    return QByteArray::fromBase64(message["fileData"].toString().toUtf8());
}

void messagePackage::setAllFile(QStringList fileName)
{
    QJsonObject json;
    for (auto name : fileName)
    {
        json.insert(name, name);
    }
    message["allFile"] = json;
}

QStringList messagePackage::allFile() const
{
    QJsonObject json = message["allFile"].toObject();
    return json.keys();
}

void messagePackage::setMeetingName(QString meetingName)
{
    message["meetingName"] = meetingName;
}

QString messagePackage::meetingName() const
{
    return message["meetingName"].toString();
}

void messagePackage::setIp(QString ip)
{
    message["ip"] = ip;
}

QString messagePackage::ip() const
{
    return message["ip"].toString();
}

void messagePackage::setPort(quint16 port)
{
    message["port"] = port;
}

quint16 messagePackage::port() const
{
    return message["port"].toInt();
}

void messagePackage::sendMessage(const messagePackage *msg, QTcpSocket *socket)
{
    QByteArray sendByte = msg->toByteArray();
    int len = sendByte.size();
    socket->write((char*)&len,sizeof(int));
    socket->write(sendByte);
}

messagePackage messagePackage::recvMessage(QTcpSocket *socket)
{
    int len ;
    socket->read((char*)&len,sizeof(int));
    QByteArray recvByte = socket->read(len);

    messagePackage message;
    message.setMessage(recvByte);
    return message;
}
